<template>
  <base-layout>
    <template #searchPanel>
      <search-panel
        v-slot="{ form }"
        :addPer="authConfig.add"
        :selectPer="authConfig.query"
        addTitle="新增供应商"
        @insert="add"
        @search="search"
        :showSearchButton="true"
        :showAddButton="true"
      >
        <job-shop v-model="form.storeId" placeholder="所属门店" />
        <!--<el-select-->
          <!--clearable-->
          <!--style="width: 200px"-->
          <!--v-model="form.supplierType"-->
          <!--placeholder="供应商类别"-->
        <!--&gt;-->
          <!--<el-option-->
            <!--v-for="item in partsDictOptions['供应商类型']"-->
            <!--:key="item.id"-->
            <!--:value="item.value"-->
            <!--:label="item.text"-->
          <!--/>-->
        <!--</el-select>-->
        <el-input
          v-model="form.supplierName"
          clearable
          placeholder="请输入供应商名称"
          style="width: 200px"
        />
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :width="160"
      >
        <template #action="{ row }">
          <i
            class="el-icon-edit"
            title="编辑"
            style="cursor: pointer"
            v-permission="authConfig.edit"
            @click="handleDataUpdateEdit(row, '编辑')"
          ></i>
          <i
            class="el-icon-delete"
            title="删除"
            style="color: red; margin-left: 20px; cursor: pointer"
            v-permission="authConfig.del"
            @click="handleDataDelete(row)"
          ></i>
        </template>
      </data-table>
    </template>
  </base-layout>
</template>
<script>
import {
  dataList,
  updateStatus,
  details,
  addData,
  updateData,
  deleteData,
} from '@/api/after-sale/systemManage/supplier'
import baseMixin from '@/mixins/base'
import { getDict } from '@/utils/index'
export default {
  mixins: [
    baseMixin({
      idName: 'id',
      findData: dataList,
      dataName: ['content', 'totalRecords'],
      addData: addData,
      updateData: updateData,
      deleteData: deleteData,
      isAdd: false,
      findDataById: details,
      format(res) {
        return res.map((data) => {
          data.businessHours = `${data.startTime}-${data.endTime}`
          if (data.status) {
            data.statusValue = "正常"
          } else {
            data.statusValue = "停用"
          }
          return data
        })
      },
    }),
  ],
  data() {
    return {
      authConfig: {
        query: 'supplier:page',
        add: 'supplier:create',
        edit: 'supplier:update',
        del: 'supplier:delete',
      },
      tableColumnAttributes: [
        { name: '所属门店', value: 'storeName', type: 'text', width: '200' },
        { name: '供应商编号', value: 'supplierCode', type: 'text', width: '200' },
        { name: '供应商名称', value: 'supplierName', type: 'text', width: '200' },
        { name: '所属类别', value: 'supplierType', type: 'text', width: '200' },
        { name: '联系人', value: 'contactName', type: 'text', width: '100' },
        { name: '联系人电话', value: 'contactPhone', type: 'text', width: '130' },
        { name: '所属城市', value: 'cityName', type: 'text', width: '100' },
        { name: '所属区域', value: 'countyName', type: 'text', width: '100' },
        { name: '详细地址', value: 'address', type: 'text', width: '200' },
        { name: '主营业务', value: 'note', type: 'text', width: '200' },
        { name: '状态', type: 'switch', switchPer: '111', valueId: 'status', value: 'statusValue', width: '100',
          action: (row) => {
            this.updateStatus(row.id)
          }
        },
        { name: '备注', value: 'remark', type: 'text', width: '200' },
        { name: '创建时间', value: 'createTime', type: 'text', width: '200' },
      ],
      partsDictOptions: [],
      types: [
        { id: '1', value: '测试收支种类1' },
        { id: '2', value: '测试收支种类2' },
      ],
    }
  },
  created() {
    this.searchOptions = {
      supplierType: '配件供应商',
    }
  },
  methods: {
    search(form) {
      this.searchOptions = {
        supplierType: '配件供应商',
        supplierName: form.supplierName,
        storeId: form.storeId
      }
      this.currentPage = 1
      this.setTableData()
    },
    add() {
      this.$router.push({ path: '/supplierManage/addSupplier' })
    },
    handleDataUpdateEdit(row, type) {
      this.$router.push({ path: '/supplierManage/editSupplier', query: { id: row.id } });
    },
    async updateStatus(id) {
      const { success } = await updateStatus(id)
      if (success) {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.setTableData()
      } else {
        this.$message.error('操作失败')
      }
    }
  },
}
</script>
